package cn.uniquesoft.api;

import cn.uniquesoft.service.*;
import cn.uniquesoft.util.FileUpUtil;
import cn.uniquesoft.vo.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/visit")
public class ApiVisitController {
    @Value("${resource.pagesize}")
    private int pagesize;
    @Value("${upload.visit}")
    private String visitPath;
    @Autowired
    private IVisitService visitService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IConsultantService consultantService;
    @Autowired
    private IDicDetailsService dicDetailsService;
    @Autowired
    private IVisitFilesService visitFilesService;

    @RequestMapping(value = "/getVisit")
    @ResponseBody
    public List<VisitVO> getVisit(int iid, String option, int iorgname) {

        List<VisitVO> itemList = new ArrayList<>();
        VisitVO item = new VisitVO();
        item.setIid(iid);
        item.setPageSize(this.pagesize);
        if (iorgname == 0) {

        } else {
            item.setIbank(iorgname);
        }
        if (option.equals("down")) {
            if (iid == 0) {
                itemList = this.visitService.getItemsFirst(item);
            } else {
                itemList = this.visitService.findListDown(item);
            }

        } else {
            itemList = this.visitService.findListUp(item);
        }
        //  SimplePropertyPreFilter filter = new SimplePropertyPreFilter(ArticleVO.class, "iid", "ctitle", "curl","dcreator");
        // String str = JSON.toJSONString(itemList, filter);
        //

        return itemList;

    }


    @RequestMapping(value = "/toSaveVisit")
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AnswerVO toSaveVisit(VisitVO item) {
        AnswerVO answerVO = new AnswerVO();
        try {
            int icompany = item.getIcompany();
            int iconsultant = item.getIconsultant();
            ConsultantVO consultantVO = this.consultantService.findOneById(iconsultant);
            CompanyVO companyVO = this.companyService.findOneById(icompany);
            item.setCcompany(companyVO.getCname());
            item.setCcompanylinkname(companyVO.getClinkman());
            item.setCcompanylinkphone(companyVO.getCphone());
            item.setIbank(consultantVO.getIbank());
            item.setCbank(consultantVO.getCbank());
            item.setCconsultant(consultantVO.getCname());
            int icompanytype = item.getIcompanytype();
            DicDetailsVO dicDetailsVO = new DicDetailsVO();
            dicDetailsVO.setCkey("visitcompanytype");
            dicDetailsVO.setCvalue(icompanytype + "");
            String cname = this.dicDetailsService.findAll(dicDetailsVO).get(0).getCname();
            item.setCcompanytype(cname);
            int i = this.visitService.save(item);
            if (i > 0) {
                answerVO.setSuccess(true);
                answerVO.setMsg("新增成功!");
                if (null != item.getBase64() && item.getBase64().length() > 0) {
                    //上传图片
                    String[] base64Arr = item.getBase64().split(",");
                    VisitFilesVO visitFilesVO;
                    for (String str : base64Arr) {
                        //上传返回文件名
                        String fileName = FileUpUtil.base64Upload(str, visitPath);
                        visitFilesVO = new VisitFilesVO();
                        visitFilesVO.setIpid(item.getIid());
                        visitFilesVO.setCurl(fileName);
                        this.visitFilesService.save(visitFilesVO);
                    }
                }
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("新增失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            answerVO.setSuccess(false);
            answerVO.setMsg("新增失败!");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//try..catch返回事务
        }
        return answerVO;
    }


}
